package com.hna.face.controller.business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hna.face.model.FaceAcls;
import com.hna.face.model.FacePersonStat;
import com.hna.face.model.FaceRecognizeResult;
import com.hna.face.model.FaceRecognizeStat;
import com.hna.face.model.FaceScheduleVehicleStat;
import com.hna.face.model.FaceVehicleStat;
import com.hna.face.model.PageInfo;
import com.hna.face.service.IFacePersonStatService;
import com.hna.face.service.IFaceRecognizeResultService;
import com.hna.face.service.IFaceRecognizeStatService;
import com.hna.face.service.IFaceScheduleStatService;
import com.hna.face.service.IFaceScheduleVehicleStatService;
import com.hna.face.service.IFaceVehicleStatService;

@Controller
public class StatController {

	@Autowired
	private IFaceRecognizeStatService recognizeStatService;

	@Autowired
	private IFacePersonStatService personStatService;
	
	@Autowired
	private IFaceVehicleStatService vehicleStatService;

	@Autowired
	private IFaceScheduleVehicleStatService sVehicleStatService;

	@RequestMapping("/stat/perDayIndex")
	private String perDayIndex() {
		return "pages/stat/perDayIndex";
	}
	@RequestMapping("/stat/perPersonDayIndex")
	private String perPersonDayIndex() {
		return "pages/stat/perPersonDayIndex";
	}
	@RequestMapping("/stat/perVehicleDayIndex")
	private String perVehicleDayIndex() {
		return "pages/stat/perVehicleDayIndex";
	}
	@RequestMapping("/stat/perS_VehicleStatDayIndex")
	private String perS_VehicleStatDayIndex() {
		return "pages/stat/perS_VehicleStatDayIndex";
	}

	@RequestMapping(value = "/rest/stat/perDayList", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> perDayList(PageInfo pageInfo, String day) {
		Map<String, Object> res = new HashMap<>();
		try {
			EntityWrapper<FaceRecognizeStat> wrapper = new EntityWrapper<>();

			if (StringUtils.isNotBlank(day)) {
				wrapper.eq("day", day);
				wrapper.orderBy("day", false);
			}

			Page<FaceRecognizeStat> page = new Page<>(pageInfo.getPage(), pageInfo.getRows());
			Page<FaceRecognizeStat> result = recognizeStatService.selectPage(page, wrapper);
			res.put("rows", result.getRecords());
			res.put("total", result.getTotal());
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}
	
	
	
	@RequestMapping(value = "/rest/stat/perPersonDayList", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> perPersonDayList(PageInfo pageInfo, String day,String personName) {
		Map<String, Object> res = new HashMap<>();
		try {
			EntityWrapper<FacePersonStat> wrapper = new EntityWrapper<>();

			if (StringUtils.isNotBlank(day)) {
				wrapper.eq("day", day);
				wrapper.orderBy("day", false);
			}
			if (StringUtils.isNotBlank(personName)) {
				wrapper.eq("person_name", personName);
				wrapper.orderBy("person_name", false);
			}

			Page<FacePersonStat> page = new Page<>(pageInfo.getPage(), pageInfo.getRows());
			Page<FacePersonStat> result = personStatService.selectPage(page, wrapper);
			res.put("rows", result.getRecords());
			res.put("total", result.getTotal());
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}
	
	
	@RequestMapping(value = "/rest/stat/perVehicleDayList", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> perVehicleDayList(PageInfo pageInfo, String day,String vehicleNo) {
		Map<String, Object> res = new HashMap<>();
		try {
			EntityWrapper<FaceVehicleStat> wrapper = new EntityWrapper<>();

			if (StringUtils.isNotBlank(day)) {
				wrapper.eq("day", day);
				wrapper.orderBy("day", false);
			}
			if (StringUtils.isNotBlank(vehicleNo)) {
				wrapper.eq("vehicle_no", vehicleNo);
				wrapper.orderBy("vehicle_no", false);
			}

			Page<FaceVehicleStat> page = new Page<>(pageInfo.getPage(), pageInfo.getRows());
			Page<FaceVehicleStat> result = vehicleStatService.selectPage(page, wrapper);
			res.put("rows", result.getRecords());
			res.put("total", result.getTotal());
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}
	
	@RequestMapping(value = "/rest/stat/perS_VehicleDayList", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> perS_VehicleDayList(PageInfo pageInfo, String day,String vehicleNo,String scheduleNo) {
		Map<String, Object> res = new HashMap<>();
		try {
			EntityWrapper<FaceScheduleVehicleStat> wrapper = new EntityWrapper<>();

			if (StringUtils.isNotBlank(day)) {
				wrapper.eq("day", day);
				wrapper.orderBy("day", false);
			}
			if (StringUtils.isNotBlank(scheduleNo)) {
				wrapper.eq("schedule_no", scheduleNo);
				wrapper.orderBy("schedule_no", false);
			}
			if (StringUtils.isNotBlank(vehicleNo)) {
				wrapper.eq("vehicle_no", vehicleNo);
				wrapper.orderBy("vehicle_no", false);
			}

			Page<FaceScheduleVehicleStat> page = new Page<>(pageInfo.getPage(), pageInfo.getRows());
			Page<FaceScheduleVehicleStat> result = sVehicleStatService.selectPage(page, wrapper);
			res.put("rows", result.getRecords());
			res.put("total", result.getTotal());
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}

}
